﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.Practices.ServiceLocation;
using Subfaculty.Common.Entities;
using Subfaculty.Common.Interfaces.Services;
using Subfaculty.WebApplication.Translators;
using Subfaculty.WebApplication.ViewModels;
using Telerik.Web.Mvc;

namespace Subfaculty.WebApplication.Controllers
{
    public class StudyAreaController : Controller
    {
    	private IStudyAreaRepository _repository;
    	private StudyAreaTranslator _translator;

    	public StudyAreaController()
		{
			_repository = ServiceLocator.Current.GetInstance<IStudyAreaRepository>();
    		_translator = new StudyAreaTranslator();
		}
        //
        // GET: /StudyArea/

		public ActionResult Index()
		{
			var StudyAreasPE = _translator.Translate(_repository.GetAll());

			return View(StudyAreasPE);
		}

		[GridAction]
		public ActionResult _Index()
		{
			var StudyAreaPEList = _translator.Translate(_repository.GetAll());

			return View("Index", new GridModel(StudyAreaPEList));
		}

		[GridAction]
		public ActionResult SelectAjaxEditing()
		{
			var StudyAreaPe = _translator.Translate(_repository.GetAll());

			return View("Index", new GridModel(StudyAreaPe));
		}

		[AcceptVerbs(HttpVerbs.Post)]
		[GridAction]
		public ActionResult SaveAjaxEditing(int id)
		{
			StudyArea StudyArea = _repository.Get(id);
			StudyAreaViewModel StudyAreaPE = _translator.Translate(StudyArea);

			TryUpdateModel(StudyAreaPE);

			_repository.Save(_translator.TranslateBack(StudyAreaPE, StudyArea));

			var StudyAreaPeList = _translator.Translate(_repository.GetAll());
			return View("Index", new GridModel(StudyAreaPeList));
		}
		[AcceptVerbs(HttpVerbs.Post)]
		[GridAction]
		public ActionResult InsertAjaxEditing()
		{
			StudyAreaViewModel pe = new StudyAreaViewModel();

			//Perform model binding (fill the product properties and validate it).
			if (TryUpdateModel(pe))
			{
				StudyArea StudyArea = new StudyArea();
				//The model is valid - insert the product.
				_repository.Save(_translator.TranslateBack(pe, StudyArea));
			}
			//Rebind the grid
			var disciplinesPeList = _translator.Translate(_repository.GetAll());
			return View("Index", new GridModel(disciplinesPeList));
		}
		[AcceptVerbs(HttpVerbs.Post)]
		[GridAction]
		public ActionResult DeleteAjaxEditing(int id)
		{
			//Find a customer with ProductID equal to the id action parameter
			StudyArea StudyArea = _repository.Get(id);
			if (StudyArea != null)
			{
				//Delete the record 
				_repository.Delete(StudyArea);
			}

			var StudyAreaPeList = _translator.Translate(_repository.GetAll());
			//Rebind the grid
			return View("Index", new GridModel(StudyAreaPeList));
		}

		public ActionResult ViewDetails(int studyAreaId)
		{
			//return Json(new { studyArea = _repository.Get(studyAreaId) });
			return PartialView("DetailTemplates/StudyAreaDetailViewModel", _translator.Translate(_repository.Get(studyAreaId)));
		}

		public JsonResult _ViewDetails(int studyAreaId)
		{
			return Json(new { studyArea = _repository.Get(studyAreaId) });
			//return PartialView("DetailTemplates/StudyAreaDetailViewModel", _translator.Translate(_repository.Get(studyAreaId)));
		}

    }
}
